﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Edu.Entity;
using DotNet.Edu.Service;
using DotNet.Edu.WebUtility;
using DotNet.Extensions;
using DotNet.Helper;
using DotNet.Mvc;
using DotNet.Utility;

namespace DotNet.EduWebApi.Controllers
{
    public class BaseApiController : JsonController
    {
        /// <summary>
        /// 获取学员信息
        /// </summary>
        /// <param name="userId">学员主键</param>
        /// <returns>返回学员信息</returns>
        protected Student GetStudentById(string userId)
        {
            return EduService.Student.Get(userId);
        }

        /// <summary>
        /// 获取分页信息
        /// </summary>
        public BoolMessage<PaginationCondition> GetPageInfo()
        {
            var pageIndex = Request.Form["page"];
            var pageSize = 10;
            var orderName = Request.Form["order"];
            var orderDir = Request.Form["dir"];

            #region 验证
            if (pageIndex.IsEmpty())
            {
                return new BoolMessage<PaginationCondition>(false, "请指定页码");
            }

            #endregion

            #region 解密

            var validResult = EncryptUtil.Decrypt(pageIndex, "页码");
            if (!validResult.Success)
            {
                return new BoolMessage<PaginationCondition>(validResult.Success, validResult.Message);
            }
            pageIndex = validResult.Data;

            if (orderName.IsNotEmpty())
            {
                validResult = EncryptUtil.Decrypt(orderName, "排序字段");
                if (!validResult.Success)
                {
                    return new BoolMessage<PaginationCondition>(validResult.Success, validResult.Message);
                }
                orderName = validResult.Data;
            }

            if (orderDir.IsNotEmpty())
            {
                validResult = EncryptUtil.Decrypt(orderDir, "排序方向");
                if (!validResult.Success)
                {
                    return new BoolMessage<PaginationCondition>(validResult.Success, validResult.Message);
                }
                orderDir = validResult.Data;
            }

            #endregion

            return new BoolMessage<PaginationCondition>(new PaginationCondition(pageIndex.ToInt(1), pageSize, orderName, orderDir));
        }
        
    }
}